package cn.peifu.hrm.consumer;

import cn.peifu.hrm.constants.BaseConstants;
import cn.peifu.hrm.to.CourseEmailTo;
import cn.peifu.hrm.to.CourseSmsTo;
import cn.peifu.hrm.to.StationMsgTo;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;


@Component
@Slf4j //打印日志
public class MessageConsumer {

    /**
     * @Payload 可以将序列化的JSON反序列化成对象
     * @param smsTo
     * @param message
     * @param channel
     */
    @RabbitListener(queues = BaseConstants.CourseConstants.QUEUE_COURSE_SMS)
    public void handleSmsMsg(@Payload CourseSmsTo smsTo, Message message, Channel channel){
        //发送消息
        log.info("发送消息 {}",smsTo);
        //手动签收
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("消费消息失败 {}",smsTo);
        }
    }

    @RabbitListener(queues = BaseConstants.CourseConstants.QUEUE_COURSE_SMS)
    public void handleEmailMsg(@Payload CourseEmailTo emailTo, Message message, Channel channel){
        //发送消息
        log.info("发送消息 {}",emailTo);
        //手动签收
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("消费消息失败 {}",emailTo);
        }
    }

    @RabbitListener(queues = BaseConstants.CourseConstants.QUEUE_COURSE_SMS)
    public void handleStationMsg(@Payload StationMsgTo stationMsgTo, Message message, Channel channel){
        //发送消息
        log.info("发送消息 {}",stationMsgTo);
        //手动签收
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("消费消息失败 {}",stationMsgTo);
        }
    }
}
